﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Office.Interop.Excel;

namespace ExcelAutomationRunner
{

    /// <summary>
    /// General purpose usage of Excel application.  Benefits from assured startup/shutdown of the COM object.
    /// </summary>
    public class ExcelApplicationFunction : ExcelApplicationBase
    {

        private Action<Object> _action;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="a">The action you want to perform with Excel</param>
        /// <returns>success, resulting file path on success; error message on failure</returns>
        public Tuple<bool, string> Work( Action<Object> a )
        {
            _action = a;
            return ProcessApplication();
        }

        protected override Tuple<bool, string> Process( Application excel )
        {
            try
            {
                _action( excel );
                return new Tuple<bool, string>( true, string.Empty );
            }
            catch ( Exception e )
            {
                return new Tuple<bool, string>( false, e.Message );
            }
        }

    }

}
